package com.training.admin.dao;

import java.util.List;

import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.core.persistence.annotation.Limit;
import com.demo2do.core.persistence.annotation.Query;
import com.demo2do.core.persistence.annotation.Variable;
import com.training.admin.entity.project.Project;
import com.training.admin.entity.project.ProjectTrainee;
import com.training.admin.entity.trainee.Trainee;
/**
 * 
 * @author Rockett
 *
 */
@Dao("hibernate")
public interface ProjectTraineeDao {
	
	/**
	 * 
	 * @param projectId
	 * @return
	 */
	@Query("FROM ProjectTrainee WHERE disabled = false and project.id =:projectId")
	public List<ProjectTrainee> listByProjectId(@Variable("projectId") Long projectId);
	/**
	 * 
	 * @param projectId
	 * @return
	 */
	@Query("FROM ProjectTrainee WHERE disabled = false and project.id =:projectId AND trainee.id =:traineeId")
	public ProjectTrainee load(@Variable("projectId") Long projectId, @Variable("traineeId") Long traineeId);

	/**
	 * @param projectId
	 * @return
	 */
	@Query("SELECT COUNT(*) FROM ProjectTrainee WHERE disabled = false and project.id =:projectId")
	public int countByProjectId(@Variable("projectId") Long projectId);
	
	/**
	 * 
	 * @param email
	 * @param project
	 * @return
	 */
	@Query("FROM ProjectTrainee WHERE disabled = false and trainee.email in :emails AND project = :project")
    public List<ProjectTrainee> listByTraineeAndProject(@Variable("emails") List<String> emails, @Variable("project") Project project);
   
	/**
	 * 
	 * @param trainee
	 * @param project
	 * @return
	 */
	@Query("FROM ProjectTrainee WHERE disabled = false and trainee = :trainee AND project = :project")
    public ProjectTrainee loadByTraineeAndProject(@Variable("trainee") Trainee trainee, @Variable("project") Project project);
   
	/**
	 * @param projectIds
	 * @return
	 */
	@Query("FROM ProjectTrainee WHERE disabled = false AND project.id in (:projectIds)")
	public List<ProjectTrainee> listByProjectIds(@Variable("projectIds") List<Long> projectIds);
	
	/**
	 * 
	 * @param name
	 * @param email
	 * @param mobile
	 * @return
	 */
	@Query("FROM ProjectTrainee WHERE  project.id =:projectId AND disabled = false AND (trainee.name LIKE :name OR trainee.email LIKE :email OR trainee.mobile LIKE :mobile) ")
	public List<ProjectTrainee> listBySearch(@Variable("name") String name, @Variable("email") String email, @Variable("mobile") String mobile, @Variable("projectId") Long projectId);
    
	/**
	 * @param project
	 * @param trainee
	 * @return
	 */
	@Query("FROM ProjectTrainee WHERE disabled = false AND project =: project AND trainee =: trainee")
	public ProjectTrainee loadByProjectAndTrainee(@Variable("project") Project project, @Variable("trainee") Trainee trainee);
   
	/**
	 * @param project
	 * @return
	 */
	@Query("FROM ProjectTrainee WHERE disabled = false AND project = :project")
	public List<ProjectTrainee> listByProject(@Variable("project") Project project);
   
	/**
	 * @param project
	 * @return
	 */
	@Query("SELECT COUNT(*) FROM ProjectTrainee WHERE disabled = false AND project =:project")
	public int countByProject(@Variable("project") Project project);
	
	/**
	 * @param project
	 * @param beginIndex
	 * @param maxResult
	 * @return
	 */
	@Limit
	@Query("FROM ProjectTrainee WHERE disabled = false AND project = :project")
    public List<ProjectTrainee> listProjectTrainee(@Variable("project") Project project, @Variable("beginIndex") int beginIndex, @Variable("maxResult") int maxResult);
	
	/**
	 * 
	 * @param trainee
	 * @return
	 */
	@Query("SELECT pt.project FROM ProjectTrainee pt WHERE pt.disabled = false AND pt.trainee = :trainee")
	public List<Project> listProject(@Variable("trainee") Trainee trainee);
	
	/**
	 * 
	 * @param trainee
	 * @param categoryIds
	 * @return
	 */
	@Query("FROM ProjectTrainee pt WHERE pt.complete = true AND pt.disabled = false AND pt.trainee = :trainee AND pt.project.secondCategory.id in (:categoryIds)")
	public List<ProjectTrainee> listTrainee(@Variable("trainee") Trainee trainee, @Variable("categoryIds") List<Long> categoryIds);
	
}
